package com.itlong.cloud.controller;


import com.itlong.cloud.POJO.DTO.smartcard.SmartCardSyncMessageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.SmartCardSyncDataTypeEnum;
import com.itlong.cloud.enumerate.ecode.AppErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.equip.smartcard.ISmartCardDeviceServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.encrypt.Md5EncryptUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *     一卡通同步-设备相关控制器.
 * </desc>
 *
 * @createDate 2017-12-15
 */
@RestController
@RequestMapping("/smartCard/syncdata/device/")
@RefreshScope
public class SmartCardDeviceController extends SmartCardBaseController {

    @Value("${" + PlatformConstants.SMART_CARD_TOKEN_KEY + "}")
    public String SMART_CARD_TOKEN_KEY;

    @Autowired
    ISmartCardDeviceServiceClient iSmartCardDeviceServiceClient;

    /**
     * <desc>
     *     一卡通推送设备信息数据到旺龙云端
     * <desc/>
     *
     * @param smartCardSyncMessageDTO 一卡通云端同步数据.
     * @return
     * @author LouisLewis
     * @createDate 2017-12-15
     */
    @LoggerInfo(operateDesc = LogDescConstant.SMART_CARD_PUSH_DEVICE)
    @RequestMapping(value = "deviceInfo/push", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO receiveDevice(SmartCardSyncMessageDTO smartCardSyncMessageDTO) {
        try {
            return this.receiveSyncDataFromSmartCard(smartCardSyncMessageDTO);
        }catch (Exception e){
            throw  new DataAccessException("【旺龙一卡通对接_设备信息】一卡通推送设备信息数据到云端" , e);
        }
    }

    /**
     * <desc>
     *     一卡通请求获取云端设备修改信息数据
     * <desc/>
     *
     * @param projectId 项目Id
     * @param lastSyncSerailNos 上一次处理成功的同步流水号列表（逗号分隔）
     * @return
     * @author zhangs
     * @createDate 2017/11/3
     */
    @LoggerInfo(operateDesc = "一卡通请求获取云端设备修改信息数据")
    @RequestMapping(value = "deviceInfo/pull", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO sendDevice(String projectId, String lastSyncSerailNos) {
        try {
            return getSmartCardSyncMessage(projectId,lastSyncSerailNos,SmartCardSyncDataTypeEnum.EQUIP_DATA);

        } catch (Exception e) {
            throw  new DataAccessException("【旺龙一卡通对接_设备信息】一卡通请求获取云端设备信息数据失败", e);
        }
    }


    /**
     * <desc>
     *     一卡通推送楼层映射表信息数据到旺龙云端
     * <desc/>
     *
     * @param smartCardSyncMessageDTO 一卡通云端同步数据.
     * @return
     * @author zhangs
     * @createDate 2017/11/3
     */
    @LoggerInfo(operateDesc = LogDescConstant.SMART_CARD_PUSH_FLOOR)
    @RequestMapping(value = "floorRelationInfo/push", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO receiveFloorData(SmartCardSyncMessageDTO smartCardSyncMessageDTO) {
        try {
            return this.receiveSyncDataFromSmartCard(smartCardSyncMessageDTO);
        }catch (Exception e){
            throw  new DataAccessException("【旺龙一卡通对接_设备信息】一卡通推送电梯楼层对应表信息数据到云端失败" , e);
        }
    }

    /**
     * <desc>
     *     一卡通从旺龙云拉取楼层映射表信息数据
     * <desc/>
     *
     * @param projectId 项目Id
     * @param lastSyncSerailNos 上一次处理成功的同步流水号列表（逗号分隔）
     * @return
     * @author Juguang.S
     * @createDate 2018/08/22
     */
    @LoggerInfo(operateDesc = LogDescConstant.SMART_CARD_PULL_RECORD)
    @RequestMapping(value = "floorRelationInfo/pull", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO sendFloorData(String projectId, String lastSyncSerailNos) {
        try {
            return getSmartCardSyncMessage(projectId,lastSyncSerailNos,SmartCardSyncDataTypeEnum.FLOOR_RELATION_DATA);
        }catch (Exception e){
            throw  new DataAccessException("【与旺龙一卡通对接_楼层映射表】一卡通从旺龙云拉取楼层映射表信息数据失败", e);
        }
    }


    /**
     * <desc>
     *     一卡通从旺龙云拉取用户通行记录
     * <desc/>
     *
     * @param projectId 项目Id
     * @param lastSyncSerailNos 上一次处理成功的同步流水号列表（逗号分隔）
     * @return
     * @author zhangs
     * @createDate 2017/10/31
     */
    @LoggerInfo(operateDesc = LogDescConstant.SMART_CARD_PULL_RECORD)
    @RequestMapping(value = "deviceRecord/pull", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO sendRecord(String projectId, String lastSyncSerailNos) {
        try {
            return getSmartCardSyncMessage(projectId,lastSyncSerailNos,SmartCardSyncDataTypeEnum.PASS_RECORD_DATA);
        }catch (Exception e){
            throw  new DataAccessException("【与旺龙一卡通对接_通行记录】一卡通请求旺龙云推送的用户通行记录失败", e);
        }
    }


    /**
     * <desc>
     *     一卡通推送通行记录信息（目前仅支持访客二维码记录 2018.01.23）数据到旺龙云端
     * <desc/>
     *
     * @param smartCardSyncMessageDTO 一卡通云端同步数据.
     * @return
     * @author LouisLewis
     * @createDate 2018-01-23
     */
    @LoggerInfo(operateDesc = LogDescConstant.SMART_CARD_PUSH_RECORD)
    @RequestMapping(value = "deviceRecord/push", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO receiveRecord(SmartCardSyncMessageDTO smartCardSyncMessageDTO) {
        try {
            return this.receiveSyncDataFromSmartCard(smartCardSyncMessageDTO);
        }catch (Exception e){
            throw  new DataAccessException("【旺龙一卡通对接_通行记录信息】一卡通推送通行记信息数据到云端" , e);
        }
    }

    /**
     * <desc>
     *     根据项目编号获取云电梯信息
     * <desc/>
     *
     * @param projectId 项目编号
     * @return
     * @author Qiang.S
     * @createDate 2019-01-26
     */
    @RequestMapping(value = "elevator/getCloudElevatorList", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO getCloudElevatorList(String projectId,String createTime,String sign) {
        try {
            //验签
            Map<String,String> params = new HashMap<>();
            params.put("projectId",projectId);
            params.put("createTime",createTime);
            if (!Md5EncryptUtil.checkAppSign(params, sign,SMART_CARD_TOKEN_KEY)){
                return  new MessageVO(AppErrorCodeEnum.APP_SIGN_ERR.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), iSmartCardDeviceServiceClient.getCloudElevatorList(projectId));
        }catch (Exception e){
            throw  new DataAccessException("【旺龙一卡通对接_根据项目编号获取云电梯信息】根据项目编号获取云电梯信息 " , e);
        }
    }

    /**
     * <desc>
     *     获取云电梯楼层映射表信息
     * <desc/>
     *
     * @param deviceUnique 设备唯一编号
     * @return
     * @author Qiang.S
     * @createDate 2019-01-26
     */
    @RequestMapping(value = "elevator/getElevatorFloorConfig", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO getElevatorFloorConfig(String deviceUnique,String createTime,String sign) {
        try {
            //验签
            Map<String,String> params = new HashMap<>();
            params.put("deviceUnique",deviceUnique);
            params.put("createTime",createTime);
            if (!Md5EncryptUtil.checkAppSign(params, sign,SMART_CARD_TOKEN_KEY)){
                return  new MessageVO(AppErrorCodeEnum.APP_SIGN_ERR.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), iSmartCardDeviceServiceClient.getElevatorFloorConfig(deviceUnique));
        }catch (Exception e){
            throw  new DataAccessException("【旺龙一卡通对接_根据项目编号获取云电梯信息】根据项目编号获取云电梯信息 " , e);
        }
    }

    /**
     * <desc>
     *      根据设备序列号获取楼层映射表
     * <desc/>
     *
     * @param deviceUnique 设备序列号
     * @return
     * @author Jiaqi.X
     * @createDate 2019/03/29
     */
    @RequestMapping(value = "elevator/getSmartCardServerFloorRelation",
            method = RequestMethod.POST ,produces = "application/json")
    @ResponseBody
    public MessageVO getSmartCardServerFloorRelation(@RequestBody String deviceUnique) {
        try {
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),
                    iSmartCardDeviceServiceClient.getSmartCardServerFloorRelation(deviceUnique));
        }catch (Exception e){
            throw  new DataAccessException("【旺龙一卡通对接_根据设备序列号获取楼层映射表】根据设备序列号获取楼层映射表 " , e);
        }
    }

    /**
     * <desc>
     *      检测设备是否为在线设备
     * <desc/>
     *
     * @param deviceUnique 设备序列号
     * @return
     * @author Jiaqi.X
     * @createDate 2019/11/11
     */
    @RequestMapping(value = "checkDeviceIsOnline", method = RequestMethod.POST ,produces = "application/json")
    @ResponseBody
    public MessageVO checkDeviceIsOnline(@RequestBody String deviceUnique) {
        try {
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),
                    iSmartCardDeviceServiceClient.checkDeviceIsOnline(deviceUnique));
        }catch (Exception e){
            throw  new DataAccessException("【旺龙一卡通对接_检测设备是否为在线设备失败】检测设备是否为在线设备失败 " , e);
        }
    }


    /**
     * <desc>
     *      获取所有设备
     * </desc>
     *
     * @param projectId
     * @return
     * @throws IOException
     * @author Jiaqi.X
     * @createDate 2019/11/25
     */
    @RequestMapping(value = "getAllDevice", method = RequestMethod.POST ,produces = "application/json")
    @ResponseBody
    public MessageVO getAllDevice(@RequestBody String projectId) throws IOException {
        try {
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), iSmartCardDeviceServiceClient.getAllDevice(projectId));
        }catch (Exception e){
            throw  new DataAccessException("【旺龙一卡通对接_获取所有设备失败】获取所有设备失败 " , e);
        }
    }
}
